#!/usr/bin/bash
rpm -qa |grep yum &>/dev/null
if [ $? -ne 0 ];then
	os_version=`cat /etc/redhat-release |awk -F ' ' '{print $4}'`
	[ -d /etc/yum.repos.d/bak ] || mkdir /etc/yum.repos.d/bak
	test -f /etc/yum.repos.d/bak/*.repo
	if [ $? -ne 0 ];then
        	mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
	fi
	version=`echo ${os_version} |awk -F '.' '{print $1}'`
	echo "downloading yum repo"
	case "$version" in
	5)
        	curl -o /etc/yum.repos.d/Centos-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo &>/dev/null
       	 	;;
	6)
       	 	curl -o /etc/yum.repos.d/Centos-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>/dev/null
        	;;
	7)
         	curl -o /etc/yum.repos.d/Centos-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>/dev/null
        	;;
	8)
         	curl -o /etc/yum.repos.d/Centos-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo &>/dev/null
        	;;
	*)
        	echo "error"

	esac
fi
gcc -v &>/dev/null
if [ $? -ne 0 ];then
	yum -y install gcc && yum -y install gcc-c++
fi
rpm -qa |grep yum-utils &>/dev/null
if [ $? -ne 0 ];then
	yum -y install yum-utils
fi
rpm -qa |grep docker &>/dev/null
if [ $? -ne 0 ];then
	yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
	if [ $? -eq 0 ];then
		systemctl start docker
	fi
else
	echo "docker is installed!"
	status=`systemctl status docker | grep "Active:" |awk -F ' ' '{print $3}' |sed 's/(\(.*\))/\1/'`
	if [ "$status" != "running" ];then
		systemctl start docker
	fi
fi
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://uhufemm8.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
